/*
 * This file is part of Catfish.
 * Copyright (C) 2010 Namazu Studios LLC
 * 
 * Catfish is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of 
 * the License, or (at your option) any later version.
 * 
 * Catfish is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with Catfish.  If not, please visit:
 *  
 * http://www.gnu.org/licenses/
 *  
 */
package com.namazustudios.catfish.utility;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * Useful for iterating over all fields of a class.
 * 
 * @author patrick
 * 
 */
public class FieldIterable implements Iterable<Field> {

    private final Deque<Field> d = new LinkedList<Field>();

    public FieldIterable(Object obj) {
        this(obj.getClass());
    }

    public FieldIterable(Class<?> cls) {
        do {
            d.addAll(Arrays.asList(cls.getDeclaredFields()));
        } while ((cls = cls.getSuperclass()) != null);
    }

    @Override
    public Iterator<Field> iterator() {
        return d.iterator();
    }

}
